<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 15:32
 */

namespace app\api\controller;

use app\common\model\Video as VideoModel;
use app\common\model\VideoPay as VideoPayModel;
use app\common\model\User as UserModel;
use app\model\VideoCateModel;
use app\service\PayService;
use app\common\model\Live;
use app\common\model\Config;
use app\service\VideoService;
use app\service\VideoSubscribeService;

/**
 * 课程收藏
 * Class Videosubscribe
 * @package app\api\controller
 */
class Videosubscribe extends Base
{
    public $videoModel;
    public $videoCateModel;
    public $videoSubscribeModel;

    public function __construct()
    {
        parent::__construct();
        $this->videoModel = new \app\common\model\Video();
        $this->videoCateModel = new VideoCateModel();
        $this->videoSubscribeModel = new \app\common\model\VideoSubscribe();
    }

    /**
     * 收藏
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function video_subscribe()
    {
        $data = input('');
        if ($data['id']) {
            $video = $this->videoModel->where('id', $data['id'])->find();
            if ($video) {
                $where = [
                    'uid' => $this->uid,
                    'video_id' => $data['id']
                ];
                $subscribe = $this->videoSubscribeModel->where($where)->find();
                if ($subscribe) {
                    $res = $this->videoSubscribeModel->where($where)->delete();
                    $this->ajax(200, '取消收藏');
                } else {
                    $save_data = [
                        'update_time' => time(),
                        'create_time' => time(),
                        'uid' => $this->uid,
                        'video_id' => $data['id']
                    ];
                    $res = $this->videoSubscribeModel->insertGetId($save_data);
                    if ($res) {
                        $this->ajax(200, '收藏成功');
                    } else {
                        $this->ajax(202, '收藏失败');
                    }
                }
            } else {
                $this->ajax(202, '课程不存在');
            }
        } else {
            $this->ajax(202, '课程不存在');
        }
    }

    /**
     * 收藏列表
     */
    public function subscribe_list()
    {
        $data = input('');
        $page = max($data['page'],1);
        $size = max($data['size'],20);
        $where = [
            'a.uid' => $this->uid,
        ];
        if ($data['title']) {
            $where['b.title'] = [
                'like', "%{$data['title']}%"
            ];
        }
        $list = VideoSubscribeService::S()->queryPage($where,$page,$size,'a.*,b.title,b.picture,b.video,b.price,b.type');
        $total = VideoSubscribeService::S()->queryCount($where);
        if($list){
            foreach ($list as $key=>$value){
                $list[$key]['picture'] = tomedia($value['picture']);
                $list[$key]['video'] = tomedia($value['video']);
            }
        }else{
            $list = [];
        }
        $this->ajax(200,'',[
            'list' =>$list,
            'total' =>$total,
            'page' =>$page,
            'size' => $size
        ]);
    }
}